package com.hsnn.medstgmini.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import com.hsnn.medstgmini.exception.MSException;
import com.hsnn.medstgmini.model.Img;

/**
 * @category 图片工具类
 * @author 蔡春龙
 * @date 2015年7月22日
 */
public class ImgUtil {
	private static String[] suffixList = { ".gif", ".jpg", ".png", ".jpeg",".bmp"};
	/**
	 * @category 校验是否是图片
	 * @date 2015年7月22日
	 * @param name
	 * @return
	 */
	public static boolean isImg(String name,String[] suffixNewList) {
		if (StringUtils.isNotBlank(name)) {
			int index = name.lastIndexOf(".");
			String suffix = name.substring(index);
			if (StringUtils.isNotBlank(suffix)) {
				suffix = suffix.toLowerCase();
				for (String temp : suffixNewList) {
					if (temp.equals(suffix)) {
						return true;
					}
				}
			}
		}
		return false;
	}
	
	/**
	 * @category 校验是否是图片
	 * @date 2015年7月22日
	 * @param name
	 * @return
	 */
	public static boolean isImg(String name) {
		if (StringUtils.isNotBlank(name)) {
			int index = name.lastIndexOf(".");
			String suffix = name.substring(index);
			if (StringUtils.isNotBlank(suffix)) {
				suffix = suffix.toLowerCase();
				for (String temp : suffixList) {
					if (temp.equals(suffix)) {
						return true;
					}
				}
			}
		}
		return false;
	}

	/**
	 * @category 获取文件后缀名
	 * @date 2015年8月27日
	 * @param name
	 * @return
	 */
	public static String getSuffix(String name) {
		String suffix = "";
		if (StringUtils.isNotBlank(name)) {
			int index = name.lastIndexOf(".");
			suffix = name.substring(index);
		}
		return suffix;
	}

	/**
	 * @category 上传资质
	 * @date 2015年9月10日
	 * @param profile
	 * @return
	 */
	public static Img uploadProfile(MultipartFile profile,String[] suffixList,String fileType) {
		Img img = new Img();

		if (profile.getSize() > 3 * 1024 * 1024) {
			throw new MSException("图片不能大于3M！");
		}

		String name = profile.getOriginalFilename();
		img.setName(name);

		if (!ImgUtil.isImg(name,suffixList)) {
			String msg = fileType;
			throw new MSException(msg);
		}

		String path = ImgUtil.class.getClassLoader().getResource("").getPath();
		path = path.replaceAll("\\\\", "/") + "../../upload/image/";

		name = UUID.randomUUID() + ImgUtil.getSuffix(name);
		String filename = path + name;
		img.setPath("/upload/image/" + name);

		File file = new File(filename);
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(file);
			fos.write(profile.getBytes());
			fos.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return img;
	}

	public static Img uploadProfile(MultipartFile profile){
		String[] suffixList =  { ".gif", ".jpg", ".png", ".jpeg",".bmp"};
		return uploadProfile(profile,suffixList, "请选择以下图片格式：gif, jpg, jpeg, png, bmp！");
	}
	
	public static Img uploadFileProfile(MultipartFile profile) {
		String[] suffixList =  { ".gif", ".jpg", ".png", ".jpeg",".bmp",".txt",".doc",".pdf",".xlsx" };
        return uploadProfile(profile,suffixList, "请选择以下图片格式：gif, jpg, jpeg, png, bmp,txt,doc,pdf,xlsx！");
	}
	
}
